//
//  AIManager.m
//  GoodDraft
//
//  Created by 李鹏程 on 2025/2/10.
// #define DeepSeek_Api_key @"sk-8f1833e9ce7540eeb4031cb958e69884"

#import "AIManager.h"

#import <AFNetworking/AFNetworking.h>


#define DeepSeek_Api_key @"sk-8f1833e9ce7540eeb4031cb958e69884"

#define TongYi_Api_key @"sk-da338451735e4a9da07ad6c1eb586219"

typedef NS_ENUM(NSInteger,AISourceType){
    AISourceTypeDeepSeek = 0,
    AISourceTypeDeepTongYi
};

@implementation AIManager

+ (void)generateTarotReadingWithCardName:(NSString *)cardName
                                 relCard:(NSString *)relCard
                                    date:(NSString *)date
                              completion:(void (^)(NSString *result, NSError *error))completion{
    
    NSString *prompt = [NSString stringWithFormat:@"你是一个专业塔罗牌今日运势解读占卜师，给两位分析。抽牌时间：%@，我们抽到的卡片分别为%@和%@。\n，请遵循以下格式返回内容：两张塔罗牌的相容性，给我们提出建议和意见\n请使用温暖,积极向上、有启发性的语气，但是要真正的遵循经典塔罗牌的含义，只返回上述内容，不要标题，不要特殊符号，禁止输出多余内容、客套话或额外说明，整体200字左右，语言：中文", date, cardName,relCard];
    [self askAi:prompt completion:completion];
    
}

+ (void)generateTarotReadingWithCardName:(NSString *)cardName
                             orientation:(NSString *)orientation
                                    date:(NSString *)date
                              completion:(void (^)(NSString *result, NSError *error))completion {
    

    NSString *prompt = [NSString stringWithFormat:@"你是一个专业塔罗牌今日运势解读占卜师。抽牌时间：%@，抽到的塔罗牌为：%@，位置：%@。\n，请遵循以下格式返回内容：象征意义一句话概述（30-40字左右）｜｜该牌的象征意义和和能量\n解读该牌在今天运势内容。\n给出具体的建议和注意事项。\n请使用温暖,积极向上、有启发性的语气，但是要真正的遵循经典塔罗牌的含义，只返回上述内容，不要标题，不要特殊符号，不要丢掉象征意义一句话概述后的｜｜，方便我程序解析，禁止输出多余内容、客套话或额外说明，整体300字左右，语言：中文", date, cardName, orientation];
    
    [self askAi:prompt completion:completion];

    
}

+ (void)askAi:(NSString *)prompt completion:(void (^)(NSString *result, NSError *error))completion{
    
    
    NSString *apiKey = DeepSeek_Api_key;
    NSString *urlString = @"https://api.deepseek.com/v1/chat/completions";
    
    NSDictionary *parameters = @{
        @"model": @"deepseek-chat",
        @"messages": @[
            @{@"role": @"system", @"content": @"你是一个专业特厉害的塔罗牌今日运势解读占卜师"},
            @{@"role": @"user", @"content": prompt}
        ],
        @"temperature": @0.7,
        @"max_tokens": @500
    };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@", apiKey] forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    [manager POST:urlString parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (![responseObject isKindOfClass:[NSDictionary class]]) {
            !completion?:completion(nil,[NSError errorWithDomain:NSCocoaErrorDomain code:1002 userInfo:@{NSLocalizedDescriptionKey:@"获取AI结果失败"}]);
            return;
        }
        
        NSArray *choices = responseObject[@"choices"];
        NSDictionary *dic = choices.firstObject;
        NSDictionary *message = dic[@"message"];
        NSString *text = message[@"content"];
        
        if(text.length == 0){
            !completion?:completion(nil,[NSError errorWithDomain:NSCocoaErrorDomain code:1002 userInfo:@{NSLocalizedDescriptionKey:@"获取AI结果失败 text = nil"}]);
        }else{
            !completion?:completion(text,nil);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        !completion?:completion(nil,error);
    }];
}

@end
